<?php
/**
 * Blog setting
 *
 * @copyright        2017 opencart.cn - All Rights Reserved
 * @link             http://www.guangdawangluo.com
 * @author           Sam Chen <sam.chen@opencart.cn>
 * @created          2017-05-22 14:00:55
 * @modified         2017-08-09 15:14:15
 */

class ControllerBlogSetting extends Controller {
	private $error = array();

	public function index() {
		$this->load->model('blog/blog');
		$this->model_blog_blog->install();

		$this->load->language('blog/blog');
		$this->document->setTitle($this->language->get('text_blog_setting'));
		$this->load->model('setting/setting');

		if (($this->request->server['REQUEST_METHOD'] == 'POST') && $this->validate()) {
			$this->model_setting_setting->editSetting('blog', $this->request->post);
			$this->session->data['success'] = $this->language->get('text_blog_setting_success');
			$this->response->redirect($this->url->link('blog/setting', 'token=' . $this->session->data['token'], true));
		}

		$data['heading_title'] = $this->language->get('text_blog_setting');

		$data['text_edit'] = $this->language->get('text_edit');
		$data['text_enabled'] = $this->language->get('text_enabled');
		$data['text_disabled'] = $this->language->get('text_disabled');
		$data['text_product'] = $this->language->get('text_product');
		$data['text_image'] = $this->language->get('text_image');
		$data['text_general'] = $this->language->get('text_general');
		$data['text_category_listing'] = $this->language->get('text_category_listing');
		$data['text_blog_home'] = $this->language->get('text_blog_home');
		$data['text_top'] = $this->language->get('text_top');
		$data['text_left'] = $this->language->get('text_left');

		$data['entry_status'] = $this->language->get('entry_status');
		$data['entry_menu_name'] = $this->language->get('entry_menu_name');
		$data['entry_default_author'] = $this->language->get('entry_default_author');
		$data['entry_post_limit'] = $this->language->get('entry_post_limit');
		$data['entry_image_category'] = $this->language->get('entry_image_category');
		$data['entry_post_image_status'] = $this->language->get('entry_post_image_status');
		$data['entry_post_image_position'] = $this->language->get('entry_post_image_position');
		$data['entry_image_post'] = $this->language->get('entry_image_post');
		$data['entry_post_description_length'] = $this->language->get('entry_post_description_length');
		$data['entry_post_read_more'] = $this->language->get('entry_post_read_more');
		$data['entry_width'] = $this->language->get('entry_width');
		$data['entry_height'] = $this->language->get('entry_height');
		$data['entry_meta_title'] = $this->language->get('entry_meta_title');
		$data['entry_meta_description'] = $this->language->get('entry_meta_description');
		$data['entry_meta_keyword'] = $this->language->get('entry_meta_keyword');

		$data['help_menu_name'] = $this->language->get('help_menu_name');
		$data['help_default_author'] = $this->language->get('help_default_author');
		$data['help_post_limit'] = $this->language->get('help_post_limit');
		$data['help_post_description_length'] = $this->language->get('help_post_description_length');

		$data['button_save'] = $this->language->get('button_save');

		if (isset($this->session->data['success'])) {
			$data['text_success'] = $this->session->data['success'];
			unset($this->session->data['success']);
		} else {
			$data['text_success'] = '';
		}

		if (isset($this->error['warning'])) {
			$data['error_warning'] = $this->error['warning'];
		} else {
			$data['error_warning'] = '';
		}

		if (isset($this->error['menu_name'])) {
			$data['error_menu_name'] = $this->error['menu_name'];
		} else {
			$data['error_menu_name'] = array();
		}

		if (isset($this->error['default_author'])) {
			$data['error_default_author'] = $this->error['default_author'];
		} else {
			$data['error_default_author'] = array();
		}

		if (isset($this->error['post_limit'])) {
			$data['error_post_limit'] = $this->error['post_limit'];
		} else {
			$data['error_post_limit'] = '';
		}

		if (isset($this->error['image_category'])) {
			$data['error_image_category'] = $this->error['image_category'];
		} else {
			$data['error_image_category'] = '';
		}

		if (isset($this->error['image_post'])) {
			$data['error_image_post'] = $this->error['image_post'];
		} else {
			$data['error_image_post'] = '';
		}

		if (isset($this->error['post_description_length'])) {
			$data['error_post_description_length'] = $this->error['post_description_length'];
		} else {
			$data['error_post_description_length'] = '';
		}

		if (isset($this->error['read_more'])) {
			$data['error_read_more'] = $this->error['read_more'];
		} else {
			$data['error_read_more'] = array();
		}

		$data['breadcrumbs'] = array();

		$data['breadcrumbs'][] = array(
			'text' => $this->language->get('text_home'),
			'href' => $this->url->link('common/dashboard', 'token=' . $this->session->data['token'], true)
		);

		$data['breadcrumbs'][] = array(
			'text' => $this->language->get('text_blog_setting'),
			'href' => $this->url->link('blog/setting', 'token=' . $this->session->data['token'], true)
		);

		$data['action'] = $this->url->link('blog/setting', 'token=' . $this->session->data['token'], true);

		if ($this->request->server['REQUEST_METHOD'] != 'POST') {
			$setting_info = $this->model_setting_setting->getSetting('blog');
		}

		if (isset($this->request->post['blog_status'])) {
			$data['blog_status'] = $this->request->post['blog_status'];
		} elseif (isset($setting_info['blog_status'])) {
			$data['blog_status'] = $setting_info['blog_status'];
		} else {
			$data['blog_status'] = 0;
		}

		if (isset($this->request->post['blog_menu_name'])) {
			$data['blog_menu_name'] = $this->request->post['blog_menu_name'];
		} elseif (isset($setting_info['blog_menu_name'])) {
			$data['blog_menu_name'] = $setting_info['blog_menu_name'];
		} else {
			$data['blog_menu_name'] = array();
		}

		$this->load->model('localisation/language');
		$data['languages'] = $this->model_localisation_language->getLanguages();

		if (isset($this->request->post['blog_default_author'])) {
			$data['blog_default_author'] = $this->request->post['blog_default_author'];
		} elseif (isset($setting_info['blog_default_author'])) {
			$data['blog_default_author'] = $setting_info['blog_default_author'];
		} else {
			$data['blog_default_author'] = array();
		}

		if (isset($this->request->post['blog_post_limit'])) {
			$data['blog_post_limit'] = $this->request->post['blog_post_limit'];
		} elseif (isset($setting_info['blog_post_limit'])) {
			$data['blog_post_limit'] = $setting_info['blog_post_limit'];
		} else {
			$data['blog_post_limit'] = 15;
		}

		if (isset($this->request->post['blog_category_image_width'])) {
			$data['blog_category_image_width'] = $this->request->post['blog_category_image_width'];
		} elseif (isset($setting_info['blog_category_image_width'])) {
			$data['blog_category_image_width'] = $setting_info['blog_category_image_width'];
		} else {
			$data['blog_category_image_width'] = 100;
		}

		if (isset($this->request->post['blog_category_image_height'])) {
			$data['blog_category_image_height'] = $this->request->post['blog_category_image_height'];
		} elseif (isset($setting_info['blog_category_image_height'])) {
			$data['blog_category_image_height'] = $setting_info['blog_category_image_height'];
		} else {
			$data['blog_category_image_height'] = 100;
		}

		if (isset($this->request->post['blog_post_image_status'])) {
			$data['blog_post_image_status'] = $this->request->post['blog_post_image_status'];
		} elseif (isset($setting_info['blog_post_image_status'])) {
			$data['blog_post_image_status'] = $setting_info['blog_post_image_status'];
		} else {
			$data['blog_post_image_status'] = 1;
		}

		if (isset($this->request->post['blog_post_image_position'])) {
			$data['blog_post_image_position'] = $this->request->post['blog_post_image_position'];
		} elseif (isset($setting_info['blog_post_image_position'])) {
			$data['blog_post_image_position'] = $setting_info['blog_post_image_position'];
		} else {
			$data['blog_post_image_position'] = 'top';
		}

		if (isset($this->request->post['theme_default_product_description_length'])) {
			$data['theme_default_product_description_length'] = $this->request->post['theme_default_product_description_length'];
		} elseif (isset($setting_info['theme_default_product_description_length'])) {
			$data['theme_default_product_description_length'] = $setting_info['theme_default_product_description_length'];
		} else {
			$data['theme_default_product_description_length'] = 100;
		}

		if (isset($this->request->post['blog_post_image_width'])) {
			$data['blog_post_image_width'] = $this->request->post['blog_post_image_width'];
		} elseif (isset($setting_info['blog_post_image_width'])) {
			$data['blog_post_image_width'] = $setting_info['blog_post_image_width'];
		} else {
			$data['blog_post_image_width'] = 840;
		}

		if (isset($this->request->post['blog_post_image_height'])) {
			$data['blog_post_image_height'] = $this->request->post['blog_post_image_height'];
		} elseif (isset($setting_info['blog_post_image_height'])) {
			$data['blog_post_image_height'] = $setting_info['blog_post_image_height'];
		} else {
			$data['blog_post_image_height'] = 180;
		}

		if (isset($this->request->post['blog_post_description_length'])) {
			$data['blog_post_description_length'] = $this->request->post['blog_post_description_length'];
		} elseif (isset($setting_info['blog_post_description_length'])) {
			$data['blog_post_description_length'] = $setting_info['blog_post_description_length'];
		} else {
			$data['blog_post_description_length'] = 200;
		}

		if (isset($this->request->post['blog_post_read_more'])) {
			$data['blog_post_read_more'] = $this->request->post['blog_post_read_more'];
		} elseif (isset($setting_info['blog_post_read_more'])) {
			$data['blog_post_read_more'] = $setting_info['blog_post_read_more'];
		} else {
			$data['blog_post_read_more'] = array();
		}

		if (isset($this->request->post['blog_meta_title'])) {
			$data['blog_meta_title'] = $this->request->post['blog_meta_title'];
		} elseif (isset($setting_info['blog_meta_title'])) {
			$data['blog_meta_title'] = $setting_info['blog_meta_title'];
		} else {
			$data['blog_meta_title'] = array();
		}

		if (isset($this->request->post['blog_meta_description'])) {
			$data['blog_meta_description'] = $this->request->post['blog_meta_description'];
		} elseif (isset($setting_info['blog_meta_description'])) {
			$data['blog_meta_description'] = $setting_info['blog_meta_description'];
		} else {
			$data['blog_meta_description'] = array();
		}

		if (isset($this->request->post['blog_meta_keyword'])) {
			$data['blog_meta_keyword'] = $this->request->post['blog_meta_keyword'];
		} elseif (isset($setting_info['blog_meta_keyword'])) {
			$data['blog_meta_keyword'] = $setting_info['blog_meta_keyword'];
		} else {
			$data['blog_meta_keyword'] = array();
		}

		$data['header'] = $this->load->controller('common/header');
		$data['column_left'] = $this->load->controller('common/column_left');
		$data['footer'] = $this->load->controller('common/footer');

		$this->response->setOutput($this->load->view('blog/setting', $data));
	}

	protected function validate() {
		if (!$this->user->hasPermission('modify', 'blog/setting')) {
			$this->error['warning'] = $this->language->get('error_permission');
		}

		foreach ($this->request->post['blog_menu_name'] as $language_id => $value) {
			if ((utf8_strlen($value) < 1) || (utf8_strlen($value) > 32)) {
				$this->error['menu_name'][$language_id] = $this->language->get('error_menu_name');
			}
		}

		foreach ($this->request->post['blog_default_author'] as $language_id => $value) {
			if ((utf8_strlen($value) < 1) || (utf8_strlen($value) > 128)) {
				$this->error['default_author'][$language_id] = $this->language->get('error_default_author');
			}
		}

		if (!$this->request->post['blog_post_limit']) {
			$this->error['post_limit'] = $this->language->get('error_post_limit');
		}

		if (!$this->request->post['blog_category_image_width'] || !$this->request->post['blog_category_image_height']) {
			$this->error['image_category'] = $this->language->get('error_image_category');
		}

		if (!$this->request->post['blog_post_image_width'] || !$this->request->post['blog_post_image_height']) {
			$this->error['image_post'] = $this->language->get('error_image_post');
		}

		if (!$this->request->post['blog_post_description_length']) {
			$this->error['post_description_length'] = $this->language->get('error_post_description_length');
		}

		foreach ($this->request->post['blog_post_read_more'] as $language_id => $value) {
			if ((utf8_strlen($value) < 1) || (utf8_strlen($value) > 128)) {
				$this->error['read_more'][$language_id] = $this->language->get('error_read_more');
			}
		}

		return !$this->error;
	}
}
